﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Entity.vo;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class AccessoryStockOut : BasePage
    {
        public Guid OutID
        {
            get
            {
                if (hiddenOutID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenOutID.Value);
            }
            set
            {
                hiddenOutID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                Unit.Text = "只";
                if (Request.QueryString["OutID"] != null)
                {
                    WsmAccessoryOutEntity StockOutEntity = new SystemBll().Get<WsmAccessoryOutEntity>(new Guid(Request.QueryString["OutID"]));
                    OutID = StockOutEntity.OutID;
                    lblRequestCode.Text = StockOutEntity.RequestCode;  //出库单号
                    OutstockDate.SelectedDate = StockOutEntity.OutStockDate;
                    Memo.Text = StockOutEntity.Memo;
                    ddloutStocktype.SelectedValue = StockOutEntity.outStocktype;
                    //employeeGUID.Value = StockInEntity.Operator.ToString();
                    //employeeName.Text = new SystemBll().Get<SysUserEntity>(StockInEntity.Operator).UserName;                    
                    DataTable dt = new StockBll().QueryStockOutDetail(OutID.ToString());
                    gridSubItems.DataSource = dt;
                    gridSubItems.DataBind();
                }
                else { PreBackdate.SelectedDate = System.DateTime.Today.AddDays(7); }
            }
        }          
        protected void lnkbtnSaveAndNew_Click(object sender, EventArgs e)
        {
            DataTable dt = getDateFromGrid(-1);
            bool isExist = false;
            for (int index = 0; index < dt.Rows.Count;index++ )
            {
                String AccessoryId = dt.Rows[index]["AccessoryID"].ToString();
                String Receiver = dt.Rows[index]["Receiver"].ToString();
                if (AccessoryId == hiddenInstrumentID.Value.Trim() && Receiver == employeeGUID.Value.ToString())
                {               
                    isExist = true;
                    break;
                }
            }
            if (!isExist)
            {
                List<WsmAccessoryStockEntity> dtDatas = new SystemBll().GetAll<WsmAccessoryStockEntity>(" and AccessoryStatus =0 and AccessoryID = '" + hiddenInstrumentID.Value.Trim() + "' order by AccessoryCode");
                foreach(WsmAccessoryStockEntity stockEntity in dtDatas)
                {
                    DataRow dr = dt.NewRow();
                    dr["AccessoryID"] = hiddenInstrumentID.Value.ToString();
                    WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(hiddenInstrumentID.Value));
                    dr["AccessoryName"] = entity.AccessoryName;
                    dr["AccessoryCompanyNum"] = entity.CompanyInstrumentCode;
                    dr["Unit"] = Unit.Text.Trim();
                    dr["Count"] = 0;
                    dr["StockID"] = stockEntity.StockID.ToString();
                    dr["AccessoryCode"] = stockEntity.AccessoryCode;
                    dr["Receiver"] = employeeGUID.Value.ToString();
                    dr["ReceiverName"] = new SystemBll().Get<SysUserEntity>(new Guid(employeeGUID.Value)).UserName;
                    dr["PreBackdate"] = System.Convert.ToDateTime( PreBackdate.SelectedDate).ToLongDateString();
;
                    dt.Rows.Add(dr);
                }
            }
            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();
        }

        private DataTable getDateFromGrid(int  i) {
            DataTable dt = new AccessoryInfo().getAccessoryStoreInfoStructure();
            for (int index = 0; index < gridSubItems.Items.Count; index++)
            {
                Label AccessoryCompanyNum = gridSubItems.Items[index].FindControl("AccessoryCompanyNum") as Label;
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label StockID = gridSubItems.Items[index].FindControl("StockID") as Label;
                Label AccessoryCode = gridSubItems.Items[index].FindControl("AccessoryCode") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                Label Unit = gridSubItems.Items[index].FindControl("Unit") as Label;
                Label Receiver = gridSubItems.Items[index].FindControl("Receiver") as Label;
                Label ReceiverName = gridSubItems.Items[index].FindControl("ReceiverName") as Label;
                //TextBox Count = gridSubItems.Items[index].FindControl("Count") as TextBox;
                Label PreBackdate = gridSubItems.Items[index].FindControl("PreBackdate") as Label;

                if (index != i)
                {
                    DataRow dr = dt.NewRow();
                    dr["AccessoryCompanyNum"] = AccessoryCompanyNum.Text;
                    dr["AccessoryID"] = AccessoryID.Text;
                    dr["StockID"] = StockID.Text;
                    dr["AccessoryName"] = AccessoryName.Text;
                    dr["Unit"] = Unit.Text;
                    //dr["Count"] = Count.Text.ToString();
                    dr["AccessoryCode"] = AccessoryCode.Text.ToString();
                    dr["Receiver"] = new Guid(Receiver.Text);
                    dr["ReceiverName"] = ReceiverName.Text;
                    dr["PreBackdate"] = PreBackdate.Text;
                    dt.Rows.Add(dr);
                }
                //else
                //{
                //    if (i > 0) 
                //    { }
                //}
            }
            return dt;
        }
        protected void link_saveBill_Click(object sender, EventArgs e)
        {
            WsmAccessoryOutEntity stockOutEntity;
            if (OutID != Guid.Empty)
            {
                stockOutEntity = new SystemBll().Get<WsmAccessoryOutEntity>(OutID);
                try
                {
                    new SystemBll().Delete<WsmAccessoryOutEntity>(" and OutID='" + OutID + "'");
                }
                catch {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "sasa", "alert('明细中已有配件发生变更,入库单更改失败')", true);
                    return;
                }
            }
            else
            {
                stockOutEntity = new WsmAccessoryOutEntity();
                stockOutEntity.RequestCode = new StockBll().GetOutBillCode();
                stockOutEntity.OutID = Guid.NewGuid();
                stockOutEntity.CreatedBy = CurrentUserID;
                stockOutEntity.CreatedOn = DateTime.Now;
                stockOutEntity.outStocktype = ddloutStocktype.SelectedValue;
                
                stockOutEntity.OutStockDate = OutstockDate.SelectedDate.Value;
            }
            stockOutEntity.Memo = Memo.Text;
            stockOutEntity.Deleted = false;
            new SystemBll().InsertOrUpdate(stockOutEntity);
            for (int index = 0; index < gridSubItems.Items.Count; index++) {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                Label StockID = gridSubItems.Items[index].FindControl("StockID") as Label;
                Label Unit = gridSubItems.Items[index].FindControl("Unit") as Label;
                Label Receiver = gridSubItems.Items[index].FindControl("Receiver") as Label;
                //TextBox Count = gridSubItems.Items[index].FindControl("Count") as TextBox;
                Label StockSource = gridSubItems.Items[index].FindControl("StockSource") as Label;
                Label PreBackdate = gridSubItems.Items[index].FindControl("PreBackdate") as Label;
                CheckBox chkAccessory = gridSubItems.Items[index].FindControl("chkAccessory") as CheckBox;
                if (chkAccessory.Checked)
                {
                    WsmStockInOutEntity entity = new WsmStockInOutEntity();
                    entity.ID = Guid.NewGuid();
                    entity.OutID = stockOutEntity.OutID;
                    entity.AccessoryID = new Guid(AccessoryID.Text.Trim());
                    entity.Quantity = 1;
                    entity.Receiver = new Guid(Receiver.Text);
                    //entity.StockSource = StockSource.Text;
                    entity.StockID = new Guid(StockID.Text);
                    WsmAccessoryStockEntity entityStock = new SystemBll().Get<WsmAccessoryStockEntity>(" and StockID = '" + entity.StockID.ToString() + "'");
                    entityStock.AccessoryStatus = 1;
                    entityStock.PreBackdate = System.Convert.ToDateTime(PreBackdate.Text);
                    new SystemBll().Update(entityStock);
                    new SystemBll().InsertOrUpdate(entity);
                }                
            }
            OutID = stockOutEntity.OutID;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "提示！", "alert('出库成功')", true);
        }
        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubItems.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubItems.DataBind();
            }
        }
        protected void gridSubItems_ItemDataBound(object sender, GridItemEventArgs e)
        {
        }

        protected void ddloutStocktype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddloutStocktype.SelectedValue == "销售单")
            { employeeGUID.Value = "F6571C44-D35A-4FBA-85FD-1A96C898D215"; employeeName.Text = "订单组"; }
            else { employeeGUID.Value = ""; employeeName.Text = ""; }
        }
    }
}
